import {newStr} from '@/utils/realmName.js'

/**
 * 定义了所有的表单验证正则
 * @param {Object} el 对象，这里是指向vue实例对象
 * @param {String} str 要验证的字符串值
 * @param {Boolean} isMust 是否为必填选项，判断如果不必须但填了值要做值得格式判断
 */

// 验证登录用户名
export function checkUsername( el, str = '', isMust = true ){
    let reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/;      // 汉字、数字、字母、下划线，下划线位置不限的组合
    if(isMust && !str){
        el.$vs.notify({title:'', text:'用户名不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的用户名格式', position:'top-right'});
        return false;
    }
    return true;
}

// 密码输入验证
export function checkPassword(el, str='', isMust = true){
    let reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/;     // 最少8位，至少1个大写字母，1个小写字母和1个数字，其他可以是任意字符
    if(isMust && !str){
        el.$vs.notify({title:'', text:'密码不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的密码格式', position:'top-right'});
        return false;
    }
    return true;
}

// 子网掩码验证
export function checkSubnetMask(el, str='', isMust = true){
    let reg = /^((128|192)|2(24|4[08]|5[245]))(\.(0|(128|192)|2((24)|(4[08])|(5[245])))){3}$/;         // 255.255.255.255
    if(isMust && !str){
        el.$vs.notify({title:'', text:'子网掩码不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的格式', position:'top-right'});
        return false;
    }
    return true;
}

// ip地址验证
export function checkIPAddress(el, str='', isMust = true){
    let reg = /^((25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d))((\/([1-9]|(1[0-9])|(2[0-9])|(3[0-2])))?)$/;  // 192.168.1.138或 192.168.0.0/24
    if(isMust && !str){
        // el.$vs.notify({title:'', text:'ip地址不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        // el.$vs.notify({title:'', text:'请填写正确的ip地址格式', position:'top-right'});
        return false;
    }
    return true;
}

// ip地址 6 校验
export function checkIP6Address(el, str='', isMust=true){
    let v6reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/gm;       
    if(isMust && !str){
        // el.$vs.notify({title:'', text:'ip地址不能为空', position:'top-right'});
        return false;
    }
    if(str && !v6reg.test(str)){
        // el.$vs.notify({title:'', text:'请填写正确的ip地址格式', position:'top-right'});
        return false;
    }
    return true;
}

// 校验域名正则
export function checkRealmName(el, str='', isMust=true){
    let reg = new RegExp("^(((ht|f)tp(s?))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.("+ newStr +")$"); 
    if(isMust && !str){
        // el.$vs.notify({title:'', text:'ip地址不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        // el.$vs.notify({title:'', text:'请填写正确的ip地址格式', position:'top-right'});
        return false;
    }
    return true;
}

//ip校验
export function isIPValid(el, ip, isMust=true) {
    let res = checkIPAddress(el, ip, isMust) || checkIP6Address(el, ip, isMust) || checkRealmName(el, ip, isMust);
    if(res){
        return true;
    }else{
        if(isMust && !ip){
            el.$vs.notify({title:'', text:'ip地址不能为空', position:'top-right'});
            return false;
        }
        el.$vs.notify({title:'', text:'请填写正确的ip地址格式', position:'top-right'});
        return false;
    }
}

// 网关地址验证
export function checkGateway(el, str='', isMust = true){
    let reg = /^192\.168(\.(\d|([1-9]\d)|(1\d{2})|(2[0-4]\d)|(25[0-5]))){2}$/;      // 网关地址   192.168.x.x
    if(isMust && !str){
        el.$vs.notify({title:'', text:'网关地址不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的网关地址格式', position:'top-right'});
        return false;
    }
    return true;
}

// mac物理地址校验
export function checkMacAddress(el, str='', isMust = true){
    let reg = /^[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}:[A-Fa-f\d]{2}$/;  // xx:xx:xx:xx:xx:xx   其中 xx 为16进制数字
    if(isMust && !str){
        el.$vs.notify({title:'', text:'mac地址不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的mac地址格式', position:'top-right'});
        return false;
    }
    return true;
}

// 端口号验证
export function checkPortNumber(el, str='', isMust = true){
    let reg = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;         // 0-65535
    if(isMust && !str){
        el.$vs.notify({title:'', text:'端口号不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的端口号', position:'top-right'});
        return false;
    }
    return true;
}

// 邮箱验证
export function checkEmailStr(el, str='', isMust = true){
    let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    if(isMust && !str){
        el.$vs.notify({title:'', text:'邮箱号不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的邮箱格式', position:'top-right'});
        return false;
    }
    return true;
}

// 校验正整数，不包括0和负整数
export function checkMinuteNumber(el, str='', isMust = true){
    let reg = /(^[1-9]\d*$)/;
    if(isMust && !str){
        el.$vs.notify({title:'', text:'分钟数不能为空', position:'top-right'});
        return false;
    }
    if(str && !reg.test(str)){
        el.$vs.notify({title:'', text:'请填写正确的格式', position:'top-right'});
        return false;
    }
    return true;
}
